home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / GGI / GGIMESA.C next >
Encoding:
C/C++ Source or Header  |  1998-07-08  |  11.2 KB  |  512 lines

  1. /* GGI-Driver for MESA
  2.  * 
  3.  * Copyright (C) 1997-1998  Uwe Maurer  -  uwe_maurer@t-online.de 
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  * ---------------------------------------------------------------------
  19.  * This code was derived from the following source of information:
  20.  *
  21.  * svgamesa.c and ddsample.c by Brian Paul
  22.  * 
  23.  */
  24.  
  25. #ifdef GGI
  26.  
  27. #include "ggimesa.h"
  28.  
  29. #undef VIS
  30. #undef FLIP
  31. #define VIS (GGIMesa->ggi_vis)
  32. #define FLIP(y) (GGIMesa->flip_y-(y))
  33.  
  34. GGIMesaContext GGIMesa = NULL;    /* the current context */
  35.  
  36. ggi_extid gl_ggi_extid;
  37.  
  38. int gl_ggi_init=GL_FALSE;
  39. int gl_ggi_debug=GL_FALSE;
  40.  
  41. static void gl_ggiUpdateState(GLcontext *ctx);
  42. static int gl_ggi_change(ggi_visual_t vis,int whatchanged);
  43.  
  44. void gl_ggiPrint(char *format,...)
  45. {
  46.     va_list args;
  47.     va_start(args,format);
  48.     fprintf(stderr,"ggimesa: ");
  49.     vfprintf(stderr,format,args);
  50.     va_end(args);
  51. }
  52.  
  53. void gl_ggiDEBUG(char *format,...)
  54. {
  55.     va_list args;
  56.     if (gl_ggi_debug)
  57.     {
  58.         va_start(args,format);
  59.         fprintf(stderr,"ggimesa: ");
  60.         vfprintf(stderr,format,args);
  61.         va_end(args);
  62.     }
  63. }
  64.  
  65. static void gl_ggiGetSize( GLcontext *ctx, GLuint *width, GLuint *height )
  66. {
  67.     *width = GGIMesa->width; 
  68.     *height = GGIMesa->height; 
  69. }
  70.  
  71. static void gl_ggiSetIndex( GLcontext *ctx, GLuint ci)
  72. {
  73.     ggiSetGCForeground(VIS,ci);
  74.     GGICTX->color=(ggi_pixel)ci;
  75. }
  76.  
  77. static void gl_ggiSetClearIndex( GLcontext *ctx, GLuint ci)
  78. {
  79.     ggiSetGCForeground(VIS,ci);
  80.     GGICTX->clearcolor=(ggi_pixel)ci;
  81. }
  82.  
  83. static void gl_ggiSetColor( GLcontext *ctx,
  84.                        GLubyte red, GLubyte green,
  85.                        GLubyte blue, GLubyte alpha )
  86. {
  87.     ggi_color rgb;
  88.     ggi_pixel col;
  89.     rgb.r= (uint16)red   << SHIFT;
  90.     rgb.g= (uint16)green << SHIFT;
  91.     rgb.b= (uint16)blue  << SHIFT;
  92.     col = ggiMapColor(VIS,&rgb);
  93.     ggiSetGCForeground(VIS,col);
  94.     GGICTX->color=col;
  95. }
  96.  
  97. static void gl_ggiSetClearColor( GLcontext *ctx,
  98.                        GLubyte red, GLubyte green,
  99.                        GLubyte blue, GLubyte alpha )
  100. {
  101.     ggi_color rgb;
  102.     ggi_pixel col;
  103.     rgb.r=(uint16)red   << SHIFT;
  104.     rgb.g=(uint16)green << SHIFT;
  105.     rgb.b=(uint16)blue  << SHIFT;
  106.     col=ggiMapColor(VIS,&rgb);
  107.     ggiSetGCForeground(VIS,col);
  108.     GGICTX->clearcolor=col;
  109. }
  110.  
  111. static GLbitfield gl_ggiClear( GLcontext *ctx,GLbitfield mask,
  112.             GLboolean all,
  113.             GLint x, GLint y, GLint width, GLint height )
  114. {
  115.  
  116.     if (mask & GL_COLOR_BUFFER_BIT) 
  117.     {
  118.         ggiSetGCForeground(VIS,GGICTX->clearcolor);
  119.  
  120.         if (all)
  121.         {
  122.             ggiDrawBox(VIS,0,GGIMesa->origin.y,
  123.                 GGIMesa->width, GGIMesa->height);
  124.         }
  125.         else
  126.         {
  127.             ggiDrawBox(VIS,x,FLIP(y),width,height);
  128.         }
  129.  
  130.         ggiSetGCForeground(VIS,GGICTX->color);
  131.     }
  132.     return mask & (~GL_COLOR_BUFFER_BIT);
  133. }
  134.  
  135. static GLboolean gl_ggiSetBuffer( GLcontext *ctx,
  136.                              GLenum mode )
  137. {
  138.     int y;
  139. /*
  140.     if (mode==GL_FRONT) 
  141.         gl_ggiPrint("GL_FRONT\n"); 
  142.     else 
  143.         gl_ggiPrint("GL_BACK\n");
  144. */
  145.     if (mode==GL_FRONT)
  146.     {
  147.          y = (GGICTX->origin.y) ? 0 : GGICTX->height;
  148.     }
  149.     else    
  150.     {
  151.          y=GGICTX->origin.y;
  152.     }
  153.  
  154.     GGICTX->flip_y = y + GGICTX->height - 1; 
  155.  
  156.     return GL_TRUE;
  157. }
  158.  
  159. static const char * gl_ggiRendererString(void)
  160. {
  161.     return "GGI";
  162. }
  163.  
  164. static void gl_ggiFlush(GLcontext *ctx)
  165. {
  166.     ggiFlush(VIS);
  167. }
  168.  
  169. static void gl_ggiSetupPointers( GLcontext *ctx )
  170. {
  171.    /* Initialize all the pointers in the DD struct.  Do this whenever */
  172.    /* a new context is made current or we change buffers via set_buffer! */
  173.  
  174.     ctx->Driver.RendererString = gl_ggiRendererString;
  175.     ctx->Driver.UpdateState = gl_ggiUpdateState;
  176.  
  177.     ctx->Driver.ClearIndex = gl_ggiSetClearIndex; 
  178.     ctx->Driver.ClearColor = gl_ggiSetClearColor;
  179.     ctx->Driver.Clear = gl_ggiClear;
  180.     ctx->Driver.Index = gl_ggiSetIndex;
  181.     ctx->Driver.Color = gl_ggiSetColor;
  182.     ctx->Driver.SetBuffer = gl_ggiSetBuffer;
  183.     ctx->Driver.GetBufferSize = gl_ggiGetSize;
  184.     ctx->Driver.Finish = gl_ggiFlush;
  185.     ctx->Driver.Flush = gl_ggiFlush;
  186.  
  187. }
  188.  
  189. static int gl_ggiInitInfo(GGIMesaContext ctx,struct ggi_mesa_info *info)
  190. {
  191.     int virty,visy,err;
  192.  
  193.     if (info->db_flag)
  194.     {
  195.         virty = ggiGetInfo(ctx->ggi_vis)->mode->virt.y;
  196.         visy =  ggiGetInfo(ctx->ggi_vis)->mode->visible.y;
  197.  
  198.         if (virty < 2 * visy)
  199.         {
  200.             gl_ggiPrint(" visible y %i, virtual y %i\n",visy,virty);
  201.             gl_ggiPrint("ggiInitInfo: no doublebuffer possible!\n");
  202.             return -1;
  203.         }
  204.     }
  205.  
  206.     info->depth_bits=DEPTH_BITS;
  207.     info->stencil_bits=STENCIL_BITS;
  208.     info->accum_bits=ACCUM_BITS;
  209.  
  210.     if (info->rgb_flag)
  211.     {        
  212.         info->rgb_flag=GL_TRUE;
  213.         info->alpha_flag=GL_FALSE;
  214.         info->index_bits=0;
  215.  
  216.         info->red_bits=8;
  217.         info->green_bits=8;
  218.         info->blue_bits=8;
  219.  
  220.         info->alpha_bits=0;
  221.     }
  222.     else
  223.     {
  224.         info->alpha_flag=GL_FALSE;
  225.  
  226.         info->index_bits=ggiGetInfo(ctx->ggi_vis)->fb.bpc;
  227.         info->red_bits=info->green_bits=info->blue_bits=
  228.                 info->alpha_bits=0;
  229.     }
  230.  
  231.     return 0;
  232. }
  233.  
  234. GGIMesaContext GGIMesaCreateContext(void)
  235. {
  236.     GGIMesaContext ctx;
  237.     char *s;
  238.  
  239.     s=getenv("GGIMESA_DEBUG");
  240.     gl_ggi_debug=(s && atoi(s));
  241.     gl_ggiDEBUG("GGIMesaCreateContext:\n");
  242.  
  243.     if (gl_ggiInit()<0)         /* register extensions*/
  244.     {
  245.         return NULL;
  246.     }
  247.  
  248.     ctx = (GGIMesaContext) calloc(1,sizeof(struct ggi_mesa_context));
  249.     if (!ctx) return NULL;
  250.     ctx->gl_vis=(GLvisual *)calloc(1,sizeof(GLvisual));
  251.     if (!ctx->gl_vis) return NULL;
  252.  
  253.     ctx->viewport_init=GL_FALSE;    
  254.     ctx->gl_vis->DBflag=GL_FALSE;
  255.  
  256.     ctx->gl_ctx = gl_create_context(ctx->gl_vis,NULL,(void *)ctx,GL_TRUE);
  257.     if (!ctx->gl_ctx) return NULL;
  258.     
  259.     gl_ggiDEBUG("GGIMesaCreateContext: done.\n");
  260.     return ctx;
  261. }
  262.  
  263. void GGIMesaDestroyContext( GGIMesaContext ctx )
  264. {
  265.     if (ctx) 
  266.     {
  267.         gl_destroy_visual(ctx->gl_vis);
  268.         gl_destroy_context(ctx->gl_ctx);
  269.         gl_destroy_framebuffer(ctx->gl_buffer);
  270.         if (ctx==GGIMesa) 
  271.             GGIMesa = NULL;
  272.         if (ctx->ggi_vis) ggiExtensionDetach(ctx->ggi_vis,gl_ggi_extid);
  273.         ggiExtensionUnregister(gl_ggi_extid);
  274.         free(ctx);
  275.     }
  276. }
  277.  
  278. int GGIMesaSetVisual( GGIMesaContext ctx,ggi_visual_t vis,
  279.             GLboolean rgb_flag,GLboolean db_flag)
  280. {
  281.     struct ggi_mesa_info info;
  282.     int err;
  283.     uint16 r,g,b;
  284.     ggi_color pal[256];
  285.     int i;
  286.     void *func;
  287.  
  288.     gl_ggiDEBUG("GGIMesaSetVisual:\n");
  289.  
  290.     if (!ctx) return -1;
  291.     if (!vis) return -1;
  292.  
  293.     if (ctx->ggi_vis) ggiExtensionDetach(ctx->ggi_vis,gl_ggi_extid);
  294.  
  295.     ctx->ggi_vis=vis;
  296.  
  297.     err=ggiExtensionAttach(vis,gl_ggi_extid);
  298.     if (err<0) return -1;
  299.     if (err==0) gl_ggi_change(vis,GGI_CHG_APILIST);
  300.     
  301.     if (ctx->gl_vis)
  302.         gl_destroy_visual(ctx->gl_vis);
  303.  
  304.     if (ctx->gl_buffer)
  305.         gl_destroy_framebuffer(ctx->gl_buffer);    
  306.  
  307.     info.rgb_flag = rgb_flag;
  308.     info.db_flag  = db_flag;
  309.  
  310.     err=gl_ggiInitInfo(ctx,&info);
  311.     if (err) return -1;
  312.  
  313.     gl_ggiSetupPointers(ctx->gl_ctx);
  314.  
  315.     func=(void*)VIS_OPMESA(ctx->ggi_vis)->setup_driver;
  316.  
  317.     if (!func) 
  318.     {
  319.         gl_ggiPrint("setup_driver==NULL !\n");
  320.         gl_ggiPrint("Please check your config files!\n");
  321.         return -1;
  322.     }
  323.  
  324.     err=VIS_OPMESA(ctx->ggi_vis)->setup_driver(ctx,&info);
  325.     if (err) return -1;
  326.  
  327.     ctx->gl_vis = gl_create_visual( info.rgb_flag,
  328.                                     info.alpha_flag,
  329.                                     info.db_flag,
  330.                     GL_FALSE,
  331.                                     info.depth_bits, 
  332.                                     info.stencil_bits,
  333.                                     info.accum_bits,
  334.                                     info.index_bits,
  335.                                        info.red_bits, info.green_bits,
  336.                                     info.blue_bits, info.alpha_bits);
  337.     if (!ctx->gl_vis) {
  338.         gl_ggiPrint("Can't create gl_visual!!!\n");
  339.         return -1;
  340.     }
  341.  
  342.     ctx->gl_buffer = gl_create_framebuffer(ctx->gl_vis);
  343.  
  344.     if (!ctx->gl_buffer) {
  345.         gl_ggiPrint("Can't create gl_buffer!!!\n");
  346.         return -1;
  347.     }
  348.  
  349.     ctx->width = ggiGetInfo(ctx->ggi_vis)->mode->visible.x;
  350.     ctx->height = ggiGetInfo(ctx->ggi_vis)->mode->visible.y;
  351.     ctx->stride = ggiGetInfo(ctx->ggi_vis)->mode->virt.x;
  352.     ctx->origin.x=0;
  353.     ctx->origin.y=(info.db_flag) ? ctx->height : 0; 
  354.     ctx->flip_y=ctx->origin.y+ctx->height-1; 
  355.  
  356.     ctx->color = 0;
  357.  
  358.     ctx->lfb=ggiGetInfo(ctx->ggi_vis)->fb.linear; /*FIXME: direct buffer*/
  359.  
  360.     ctx->gl_ctx->Visual=ctx->gl_vis;
  361.     ctx->gl_ctx->Pixel.ReadBuffer= 
  362.     ctx->gl_ctx->Color.DrawBuffer= (db_flag) ? GL_BACK : GL_FRONT;
  363.  
  364.     if (GGIMesa==ctx)
  365.         gl_make_current(ctx->gl_ctx, ctx->gl_buffer);
  366.  
  367.     if (rgb_flag && ggiGetInfo(ctx->ggi_vis)->mode->graphtype==GT_8BIT)
  368.     {
  369.  
  370.         for (i=r=0;r<8;r++)
  371.             for (g=0;g<8;g++)
  372.                 for (b=0;b<4;b++,i++)
  373.                 {
  374.                     pal[i].r=r<<(GGI_COLOR_PRECISION-3);    
  375.                     pal[i].g=g<<(GGI_COLOR_PRECISION-3);    
  376.                     pal[i].b=b<<(GGI_COLOR_PRECISION-2);    
  377.                 }
  378.         ggiSetPaletteVec(ctx->ggi_vis,0,256,pal);    
  379.     }
  380.  
  381.     gl_ggiDEBUG("GGIMesaSetVisual: done.\n");
  382.     return 0;
  383. }
  384.  
  385. void GGIMesaMakeCurrent( GGIMesaContext ctx )
  386. {
  387.     if (!ctx->ggi_vis) return;
  388.     GGIMesa = ctx;
  389.     gl_make_current( ctx->gl_ctx, ctx->gl_buffer );
  390.     
  391.     if (!ctx->viewport_init)
  392.     {
  393.         gl_Viewport( ctx->gl_ctx, 0, 0, ctx->width, ctx->height );
  394.         ctx->viewport_init=GL_TRUE;
  395.     }
  396. }
  397.  
  398. GGIMesaContext GGIMesaGetCurrentContext( void )
  399. {
  400.     return GGIMesa;
  401. }
  402.  
  403. /*
  404.  * Swap front/back buffers for current context if double buffered.
  405.  */
  406. void GGIMesaSwapBuffers( void )
  407. {
  408.     if (GGIMesa->gl_vis->DBflag) 
  409.     {
  410.         ggiSetOrigin(VIS,GGIMesa->origin.x,GGIMesa->origin.y);
  411.  
  412.         if (GGIMesa->origin.y) GGIMesa->origin.y=0;
  413.         else GGIMesa->origin.y=GGIMesa->height;
  414.         
  415.         GGIMesa->flip_y=GGIMesa->origin.y+GGIMesa->height-1;  
  416.     }
  417. }
  418.  
  419. static void gl_ggiLoadLibrary(ggi_visual_t vis,char *driver,char *args)
  420. {
  421.     char str[1024];
  422.  
  423.     strncpy(str,driver,1000);
  424.     strcat(str,"-mesa");
  425.  
  426.     if (ggiLoadLibrary(vis,str,args)) {
  427.         gl_ggiDEBUG("vis %p: \"%s\" loaded!\n",vis,str);
  428.     }
  429.     else
  430.     {
  431.         if (strcmp(driver,"generic-stubs")==0)
  432.         {
  433.             gl_ggiPrint("%s not found!\n",str);
  434.             gl_ggiPrint("Please check your config files.\n");
  435.             ggiPanic("");
  436.         }
  437.     }
  438. }
  439.  
  440. static int gl_ggi_change(ggi_visual_t vis,int whatchanged)
  441. {
  442.     char api[1024];
  443.     char args[1024];
  444.     int i;
  445.  
  446.     gl_ggiDEBUG("gl_ggi_change:\n");
  447.     switch (whatchanged)
  448.     {
  449.         case GGI_CHG_APILIST:
  450.         {
  451.             gl_ggiDEBUG("\tGGI_CHG_APILIST:\n");
  452.             for (i=0;ggiGetAPI(vis,i,api,args)==0;i++)
  453.             {
  454.                 gl_ggiDEBUG("----> \"%s\" - \"%s\"\n",api,args);
  455.                 gl_ggiLoadLibrary(vis,api,args);
  456.             }
  457.             
  458.         } break;
  459.         default:
  460.         gl_ggiPrint("gl_ggi_changed: unknown value %i\n",whatchanged);
  461.         break;
  462.     }
  463.     gl_ggiDEBUG("gl_ggi_change: done.\n");
  464.     return 0;
  465. }
  466.  
  467. int gl_ggiInit()
  468. {
  469.     if (gl_ggi_init) return 0;
  470.  
  471.  
  472.     gl_ggi_extid=ggiExtensionRegister("GGI-Mesa",sizeof(struct ggi_mesa_op),
  473.                 gl_ggi_change);
  474.     if (gl_ggi_extid>=0) 
  475.     {
  476.         gl_ggiDEBUG("Registered extension %i\n",gl_ggi_extid);
  477.         gl_ggi_init=GL_TRUE;
  478.         return 0;
  479.     }
  480.  
  481.     gl_ggiPrint("Can't register extension!\n");
  482.     return -1;    
  483. }
  484.  
  485. static void gl_ggiUpdateState(GLcontext *ctx)
  486. {
  487.     void *func;
  488.  
  489.     func=(void *)CTX_OPMESA(ctx)->update_state;
  490.  
  491.     if (!func) {
  492.         gl_ggiPrint("update_state==NULL !\n");
  493.         gl_ggiPrint("Please check your config files!\n");
  494.         ggiPanic("");
  495.     }
  496.  
  497.     CTX_OPMESA(ctx)->update_state(ctx);
  498. }
  499.  
  500. #else
  501. /*
  502.  * Need this to provide at least one external definition when GGI is
  503.  * not defined on the compiler command line.
  504.  */
  505.  
  506. int gl_ggi_dummy_function(void)
  507. {
  508.     return 0;
  509. }
  510.  
  511. #endif
  512.